package com.wxp.entity;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;

import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.hibernate.annotations.Cascade;
import org.hibernate.annotations.CascadeType;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.validator.constraints.Length;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.modules.model.SimpleModel;


@Entity
@Table(name = "t_role")
public class Role extends SimpleModel{
	
	@Id
	@Length(max=64)
	@Column(name="ROLE_CODE_" ,nullable = false ,unique = true ,insertable = true ,updatable = true,length=64)
	 @GeneratedValue(generator = "uuid")
    @GenericGenerator(name = "uuid", strategy = "com.modules.utils.IDGenerator")
	private String roleCode;
	
	
	@Length(max=64)
	@Column(name="ROLE_NAME_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=64)
	private String roleName;
	
	
	
	@Length(max=512)
	@Column(name="ROLE_DESC_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=512)
	private String roleDesc;
	
	
	@Length(max=64)
	@Column(name="CHECKED_" ,nullable = true ,unique = false ,insertable = true ,updatable = true,length=64)
	private String checked;
	
	@OneToMany(fetch=FetchType.LAZY,mappedBy="role")
	@Cascade(value={CascadeType.SAVE_UPDATE} )	
	@JsonIgnore
	private Set<Permission> permission = new HashSet<Permission>();
	
	
	
	
	public String getRoleCode() {
		return roleCode;
	}
	public void setRoleCode(String roleCode) {
		this.roleCode = roleCode;
	}
	public String getRoleName() {
		return roleName;
	}
	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}
	public String getRoleDesc() {
		return roleDesc;
	}
	public void setRoleDesc(String roleDesc) {
		this.roleDesc = roleDesc;
	}
	
	
	
	public Set<Permission> getPermission() {
		return permission;
	}
	
	
	public void setPermission(Set<Permission> permission) {
		this.permission = permission;
	}
	
	
	
	
	public String getChecked() {
		return checked;
	}
	public void setChecked(String checked) {
		this.checked = checked;
	}
	@Transient
	public String getPermissionString() {
		int i=0;
		
		if(permission.size()>0) {
			String name ="";
			for(Permission value: permission){
				if(i<5) {
					name = name+value.getOperatorName()+",";
					i++;
				}else {
					return name.substring(0, name.lastIndexOf(","))+"....";
				}
	        }
			return name.substring(0, name.lastIndexOf(","));
		}else {
			return "";
		}
		
	}
	
	@Override
	public String toString() {
		return "Role [roleCode=" + roleCode + ", roleName=" + roleName + ", roleDesc=" + roleDesc + "]";
	}
	@Override
	public int hashCode() {
		return new HashCodeBuilder()
				.append(getRoleCode())
				.toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		if(obj instanceof Role == false) return false;
		if(this == obj) return true;
		Role other = (Role)obj;
		return new EqualsBuilder()
			.append(getRoleCode(),other.getRoleCode())
			.isEquals();
	}

}
